#!/bin/bash

if [ $# -ne 1 ];then
	echo "Usage: $0 obs_pro_name"
	echo "Example: $0 Mainline/20.09/Factory"
	exit 1
else
	if [ -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo ];then
		rm -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo >/dev/null
	fi
	name=`date +%F-%R`
	echo "The Programs running at Time:`date`"
	if [[ "$1" =~ "Mainline" ]];then
		printf "###########\t\tcheck $1 install rely log\t\t###########\n" >> ${name}_log
		yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/Mainline/standard_aarch64/ &>/dev/null
		dnf repoclosure --repo 119.3.219.20_82_openEuler_Mainline_standard_aarch64_ --arch aarch64 --arch noarch &>/dev/null >> ${name}_log
		yum list --repo 119.3.219.20_82_openEuler_Mainline_standard_aarch64_ | grep ".src" > ${name}_src_pkglist
		echo "You can see log in file ${name}_log and ${name}_src_pkglist"
		cat ${name}_log | grep -Ev "package:|unresolved" > nothing_provides_log
		sed -i 's/^[ ]*//g' nothing_provides_log
		sed -i '1,2d' nothing_provides_log
		cat nothing_provides_log | while read line
		#for line in `cat nothing_provides_log`
		do
			dnf repoquery --whatprovides "$line" &>/dev/null > res
			wpname=$(cat res)
			# dnf repoquery --whatprovides return null
			if [ -z "$wpname" ] &>/dev/null;then
				echo "$line" >> nothing_provides_source_rpm
			else
				#according binary pkgname to Find source pkgname
				dnf repoquery -i "${wpname%%-[0-9]*}" | grep Source | head -1 >> nothing_provides_source_rpm
			fi
			rm -f res >/dev/null
		done
		exit 0
	elif [[ "$1" =~ "20.09" ]];then
		printf "###########\t\tcheck $1 install rely log\t\t###########\n" >> ${name}_log
		yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/20.09/standard_aarch64/ &>/dev/null
		dnf repoclosure --repo 119.3.219.20_82_openEuler_20.09_standard_aarch64_ --arch aarch64 --arch noarch &>/dev/null >> ${name}_log
		yum list --repo 119.3.219.20_82_openEuler_20.09_standard_aarch64_ | grep ".src" > ${name}_src_pkglist
		rm -f /etc/yum.repos.d/119.3.219.20_82_openEuler_$1_standard_aarch64_.repo &>/dev/null
		echo "You can see log in file ${name}_log and ${name}_src_pkglist"
		cat ${name}_log | grep -Ev "package:|unresolved" > nothing_provides_log
		sed -i 's/^[ ]*//g' nothing_provides_log
		sed -i '1,2d' nothing_provides_log
		cat nothing_provides_log | while read line
		#for line in `cat nothing_provides_log`
		do
			dnf repoquery --whatprovides "$line" &>/dev/null > res
			wpname=$(cat res)
			# dnf repoquery --whatprovides return null
			if [ -z "$wpname" ] &>/dev/null;then
				echo "Source       : $line" >> ${name}_nothing_provides_source_rpm
			else
				#according binary pkgname to Find source pkgname
				dnf repoquery -i "${wpname%%-[0-9]*}" | grep Source | head -1 >> ${name}_nothing_provides_source_rpm
			fi
			rm -f res >/dev/null
		done
		rm -f nothing_provides_log &>/dev/null
		exit 0
	elif [[ "$1" =~ "Factory" ]];then
		printf "###########\t\tcheck $1 install rely log\t\t###########\n" >> ${name}_log
		yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/Factory/standard_aarch64/ &>/dev/null
		dnf repoclosure --repo 119.3.219.20_82_openEuler_Factory_standard_aarch64_ --arch aarch64 --arch noarch &>/dev/null >> ${name}_log
		yum list --repo 119.3.219.20_82_openEuler_Factory_standard_aarch64_ | grep ".src" > ${name}_src_pkglist
		echo "You can see log in file ${name}_log and ${name}_src_pkglist"
		cat ${name}_log | grep -Ev "package:|unresolved" > nothing_provides_log
		sed -i 's/^[ ]*//g' nothing_provides_log
		sed -i '1,2d' nothing_provides_log
		cat nothing_provides_log | while read line
		#for line in `cat nothing_provides_log`
		do
			dnf repoquery --whatprovides "$line" &>/dev/null > res
			wpname=$(cat res)
			# dnf repoquery --whatprovides return null
			if [[ -z "$wpname" ]]&>/dev/null;then
				echo "Source       : $line" >> nothing_provides_source_rpm
			else
				#according binary pkgname to Find source pkgname
				dnf repoquery -i "${wpname%%-[0-9]*}" | grep Source | head -1 >> nothing_provides_source_rpm
			fi
			rm -f res >/dev/null
		done
		exit 0
	else
		echo "openEuler:$1 obs project not exist......"
		exit 0
	fi
fi
#	yum-config-manager --add-repo http://119.3.219.20:82/openEuler:/20.09/standard_aarch64/ >/dev/null
#	dnf repoclosure --repo 119.3.219.20_82_openEuler_20.09_standard_aarch64_ --arch aarch64 --arch noarch >> ${name}
